<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$gametype = intval(getvar("gametype"));
if ($curruser['prev_id'] != 0)
{
    exit();
}
$gType = chkGametype($gametype);
switch ($gType)
{
    case 0 :
        $ui = new UI();
        include( "admin_menu.php" );
        break;
    default :
        include_once( "class_3d/CheckMode_3d.php" );
        $chk = new CheckMode_3d($curruser['acl'], 1, $gametype);
        $chk->checkShow3dPage();
        unset($chk);
        $ui = new UI_3D();
        $jslink = "<script language=\"JScript.Encode\" src=\"site.js\"></script>" . ( "<script language=\"VBScript.Encode\" src=\"site.vbs\"></script>" );
        $ui->set("jslink", $jslink);
        $ui->header();
        include( "admin_menu_3d.php" );
        break;
}
$conf = new Config();
if ($conf->get("calcbet") == 1 || $conf->get("calcbet_3d") == 1 || $conf->get("calcbet_3t") == 1 || $conf->get("calcbet_3s1") == 1 || $conf->get("calcbet_3s2") == 1 || $conf->get("calcbet_3s3") == 1 || $conf->get("calcbet_3s4") == 1 || $conf->get("calcbet_3s5") == 1 || $conf->get("calcbet_3s6") == 1 || $conf->get("calcbet_5h") == 1 || $conf->get("calcbet_8h") == 1 || $conf->get("calcbet_8h2") == 1 || $conf->get("calcbet_8h3") == 1)
{
    $ui->showErrorPage("<p class=error>已经在颁奖过帐中，无法删除注单！</p>");
    exit();
}
$dt1 = getvar("dt1");
$dt2 = getvar("dt2");
$dt3 = "";
if (isset($_POST['dt1']))
{
    $OnlyBettotal = isset($_POST['chkbxOnlyBettotal']);
    $qchkbx = isset($_POST['qchkbx']);
    if ($qchkbx)
    {
        $dt1 = date("Y-m-d", strtotime("-45 day"));
        $dt2 = date("Y-m-d");
    }
    $dt3 = date("Y-m-d", strtotime("-120 day"));
    echo "<br>清除" . ( $qchkbx ? "全部" : "{$dt1} ～ {$dt2} 期间" ) . "的异动数据及所产生的资料表～</br>";
    foreach ($g_gametype_3d as $gameid => $val)
    {
        $tbl = $gameid ? "_" . $g_gametype_3d[$gameid]['en'] : "";
        $conti = "(open BETWEEN '{$dt1}' AND '{$dt2}') OR open='0000-00-00'";
        getdb("delete FROM lotto{$tbl} WHERE open<'{$dt3}'");
        $rs = 0;
        if (1 < $val['type'])
        {
            echo "<br>Select open From lotto{$tbl} where " . $conti . " GROUP BY open ORDER BY open</br>";
            $rs = getdb("Select open From lotto{$tbl} where (open BETWEEN '{$dt1}' AND '{$dt2}') GROUP BY open ORDER BY open");
        }
        else
        {
            echo "<br>Select id From lotto{$tbl} where " . $conti . " ORDER BY open</br>";
            $rs = getdb("Select id From lotto{$tbl} where (open BETWEEN '{$dt1}' AND '{$dt2}') ORDER BY open");
        }
        if (!$rs->eof)
        {
            echo "<br>TRUNCATE TABLE autorate{$tbl}</br>";
            getdb("TRUNCATE TABLE autorate{$tbl}");
            if ($qchkbx)
            {
                echo "<br>TRUNCATE TABLE xbet{$tbl}</br>";
                getdb("update lotto{$tbl} set amount=0, mb=0, ad=0, ua=0, ur=0, rsum=0 , note=NULL WHERE id>0");
                getdb("TRUNCATE TABLE " . ( $gameid == 0 ? "bet" : "xbet" . $tbl ));
                getdb("TRUNCATE TABLE " . ( $gameid == 0 ? "bet2" : "xbet2" . $tbl ));
                getdb("TRUNCATE TABLE " . ( $gameid == 0 ? "bet8" : "xbet8" . $tbl ));
                getdb("TRUNCATE TABLE k_user_bet");
                if ($gameid == 0)
                {
                    $k = 1;
                    while ($k < 5)
                    {
                        getdb("delete FROM rate0" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate1" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate4" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate5" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate15" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate24" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate31" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate41" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate42" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate43" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate44" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate45" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        getdb("delete FROM rate46" . $g_disk_code[$k] . " WHERE lotto_id>0");
                        ++$k;
                    }
                    getdb("delete FROM rate8 WHERE lotto_id>0");
                    getdb("delete FROM rate27 WHERE lotto_id>0");
                    getdb("delete FROM rate29 WHERE lotto_id>0");
                    getdb("delete FROM rate36 WHERE lotto_id>0");
                    echo "<br>TRUNCATE TABLE rate0s ～ rate46s</br>";
                    getdb("TRUNCATE TABLE rate0s");
                    getdb("TRUNCATE TABLE rate1s");
                    getdb("TRUNCATE TABLE rate4s");
                    getdb("TRUNCATE TABLE rate5s");
                    getdb("TRUNCATE TABLE rate15s");
                    getdb("TRUNCATE TABLE rate41s");
                    getdb("TRUNCATE TABLE rate42s");
                    getdb("TRUNCATE TABLE rate43s");
                    getdb("TRUNCATE TABLE rate44s");
                    getdb("TRUNCATE TABLE rate45s");
                    getdb("TRUNCATE TABLE rate46s");
                }
                else
                {
                    echo "<br>TRUNCATE TABLE rate{$tbl}</br>";
                    getdb("TRUNCATE TABLE rates" . $tbl);
                    getdb("delete FROM rate" . $tbl . " WHERE lotto_id>0");
                    getdb("delete FROM ratenum" . $tbl . " WHERE lotto_id>0");
                    getdb("TRUNCATE TABLE ratenums" . $tbl);
                }
            }
            else if (!$OnlyBettotal)
            {
                echo "<br>DELETE FROM " . ( $gameid == 0 ? "bet" : "xbet" . $tbl ) . " WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE " . $conti . ")</br>";
                getdb("update lotto{$tbl} set amount=0, mb=0, ad=0, ua=0, ur=0, rsum=0 ,note=NULL WHERE " . $conti);
                getdb("DELETE FROM " . ( $gameid == 0 ? "bet" : "xbet" . $tbl ) . " WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE " . $conti . ")");
                getdb("DELETE FROM " . ( $gameid == 0 ? "bet2" : "xbet2" . $tbl ) . " WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE " . $conti . ")");
                getdb("DELETE FROM " . ( $gameid == 0 ? "bet8" : "xbet8" . $tbl ) . " WHERE lotto_id IN (SELECT id FROM lotto{$tbl} WHERE " . $conti . ")");
                getdb("TRUNCATE TABLE k_user_bet");
                if ($gameid == 0)
                {
                    $k = 1;
                    while ($k < 5)
                    {
                        $id_list = "(Select id From lotto where (open BETWEEN '{$dt1}' AND '{$dt2}') ORDER BY id)";
                        getdb("delete FROM rate0" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate1" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate4" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate5" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate15" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate24" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate31" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate41" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate42" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate43" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate44" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate45" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        getdb("delete FROM rate46" . $g_disk_code[$k] . " WHERE lotto_id in " . $id_list);
                        ++$k;
                    }
                    getdb("delete FROM rate8  WHERE lotto_id in " . $id_list);
                    getdb("delete FROM rate27 WHERE lotto_id in " . $id_list);
                    getdb("delete FROM rate29 WHERE lotto_id in " . $id_list);
                    getdb("delete FROM rate36 WHERE lotto_id in " . $id_list);
                    echo "<br>TRUNCATE TABLE rate0s ～ rate46s</br>";
                    getdb("TRUNCATE TABLE rate0s");
                    getdb("TRUNCATE TABLE rate1s");
                    getdb("TRUNCATE TABLE rate4s");
                    getdb("TRUNCATE TABLE rate5s");
                    getdb("TRUNCATE TABLE rate15s");
                    getdb("TRUNCATE TABLE rate41s");
                    getdb("TRUNCATE TABLE rate42s");
                    getdb("TRUNCATE TABLE rate43s");
                    getdb("TRUNCATE TABLE rate44s");
                    getdb("TRUNCATE TABLE rate45s");
                    getdb("TRUNCATE TABLE rate46s");
                }
                else
                {
                    echo "<br>TRUNCATE TABLE rates" . $tbl . "</br>";
                    getdb("TRUNCATE TABLE rates" . $tbl);
                    getdb("delete FROM rate" . $tbl . " WHERE lotto_id IN (SELECT id FROM lotto" . $tbl . " WHERE " . $conti . ")");
                    getdb("delete FROM ratenum" . $tbl . " WHERE lotto_id IN (SELECT id FROM lotto" . $tbl . " WHERE " . $conti . ")");
                    getdb("TRUNCATE TABLE ratenums" . $tbl);
                }
            }
        }
        $tblna = "";
        while (!$rs->eof)
        {
            if (1 < $val['type'])
            {
                $open = $rs->f['open'];
                $openTempArr = explode("-", $open);
                $tblna = "bettotal" . $tbl . "_" . $openTempArr[0] . $openTempArr[1] . $openTempArr[2];
            }
            else
            {
                $tblna = "bettotal" . $tbl . "_" . $rs->f['id'];
            }
            echo "<br>SELECT count(table_name) FROM Information_schema.tables WHERE table_name = '" . $tblna . "' AND table_schema ='" . G_DBDATABASE . "'</br>";
            $rs1 = getdb("SELECT count(table_name) FROM Information_schema.tables WHERE table_name = '" . $tblna . "' AND table_schema ='" . G_DBDATABASE . "'");
            if (0 < $rs1->f[0])
            {
                echo "<br>DROP TABLE " . $tblna . "</br>";
                getdb("DROP TABLE " . $tblna);
            }
            $rs->next();
        }
    }
    if (!$OnlyBettotal)
    {
        getdb("UPDATE `k_config` SET value='0' WHERE id='member_max_limit' AND ap='' LIMIT 1");
        getdb("DELETE FROM maxbet0 WHERE user_id>0");
        getdb("TRUNCATE TABLE exceptional_log");
        getdb("TRUNCATE TABLE modifybook");
        getdb("TRUNCATE TABLE rate_record");
        getdb("DELETE FROM `k_log` WHERE FROM_UNIXTIME(`logindt`,'%Y-%m-%d') < DATE_SUB(curdate(),INTERVAL 30 DAY)");
        getdb("TRUNCATE TABLE dblog");
        $conf->set("paper_from_date", $dt2);
    }
    echo "<br><p><font color=red><b>资料表删除完成！</b></font></p>";
}
else
{
    $ui->load("calendar_js", "calendar_js.htm");
    $ui->show("calendar_js");
    echo "<form action=\"op.php?op=admin&fp=dbdel\" method=post >";
    echo "<table border=1 cellpadding=0 cellspacing=0>";
    echo "<tr class=td0><td>设定日期";
    echo "<td class=td2><br>";
    $dt = date("Y-m-d", strtotime("-60 day"));
    echo "<input size=10 name=dt1 value='" . $dt . "' id=\"dt1\" /> ";
    echo "<a href=\"#\" onclick=\"javascript:showCalendarById('dt1')\"><img align=absmiddle src=\"class/calendar/calendar.png\"  alt=\"开启小月历\"  border=0></a>";
    echo " ～ ";
    $dt = date("Y-m-d", strtotime("-30 day"));
    echo "<input size=10 name=dt2 value='" . $dt . "' id=\"dt2\" /> ";
    echo "<a href=\"#\" onclick=\"javascript:showCalendarById('dt2')\"><img align=absmiddle src=\"class/calendar/calendar.png\"  alt=\"开启小月历\"  border=0></a>";
    echo "&#12288;&#12288;&#12288;&#12288;&#12288;";
    echo "<input type=checkbox name=qchkbx>清空全部的注单资料";
    echo "<p><input type=checkbox name=chkbxOnlyBettotal checked>仅删除该日期区间内 Bettotal 统计资料表";
    echo "<p>※ 依设定日期,删除该日期区间(含)的所有注单资料表,包含六合,褔彩,体彩,时时彩等。</p>";
    echo "<p>※ 若无勾选「清空全部」则依指定期间清除 xbet* & xbet2* & xbet8* & bettotal* 等资料表内容！&#12288;</p>";
    echo "<p>※ 若勾选「仅删除该日期区间内 Bettotal 统计资料表」则依指定期间清除 bettotal* & xbet* & xbet2* & xbet8* 等资料表内容！&#12288;</p>";
    echo "<input type=hidden name=gametype value=\"{$gametype}\">";
    echo "<p><input type=submit value=确定> <input type=button value=放弃 onclick=\"location.href='op.php?op=admin&fp=dbdel&gametype={$gametype}'\">";
    echo "</form>";
}
echo "</body></html>";
?>